package list;

/**
 * @description:
 * @author: qian
 * @createDate: 2021/9/28
 */
public class LinkList<E> extends AbstractList<E> {

    private Node<E> first;


    private static class Node<E>{

        E element;

        Node<E> next;

        public Node(E element,Node<E> next) {
            this.element = element;
            this.next = next;
        }
    }


    @Override
    public void clear() {

        size = 0;
        first = null;
    }





    @Override
    public boolean contains(E element) {
        return false;
    }


    @Override
    public E get(int index) {
        return node(index).element;
    }

    @Override
    public E set(int index, E element) {

        Node<E> node = node(index);
        E old = node.element;
        node.element = element;

        return old;
    }

    @Override
    public void add(int index, E element) {

        rangeCheckForAdd(index);
        if (index == 0) {

            first = new Node<>(element, first);
        } else {
            Node<E> pre = node(index - 1);
            Node<E> node = new Node<>(element, pre.next);
            pre.next = node;

        }
        size++;

    }

    @Override
    public E remove(int index) {

        rangeCheck(index);
        Node<E> node = first;
        if (index == 0) {
            first = first.next;
        }
        else {
            Node<E> pre = node(index - 1);
            node = pre.next; // 要删除的节点
            pre.next = pre.next.next;
        }

        size--;
        return node.element;
    }

    @Override
    public int indexOf(E element) {
        Node<E> node = first;
        if (element == null) {
            for(int i = 0; i < size; i++) {
                if (node.element == null) {
                    return i;
                }
                node = node.next;
            }
        } else {
            for(int i = 0; i < size; i++) {
                if (node.element.equals(element)) {
                    return i;
                }
                node = node.next;
            }
        }

        return ELEMENT_NOT_FOUND;
    }

    private Node<E> node(int index) {

        rangeCheck(index);

        Node<E> node = first;
        for (int i = 0; i < index; i++) {

            node = node.next;
        }
        return node;
    }

    @Override
    public String toString() {
        StringBuilder string = new StringBuilder();
        string.append("[size=").append(size).append(", ");
        Node<E> node = first;
        for (int i = 0; i < size; i++) {
            if (i != 0) {
                string.append(", ");
            }
            string.append(node.element);
            node = node.next;
        }
        string.append("]");
        return string.toString();
    }

}
